{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "afd55886-5f5b-4794-838e-ef8179fb0394",
   "metadata": {},
   "source": [
    "##### **** These pip installs need to be adapted to use the appropriate release level. Alternatively, The venv running the jupyter lab could be pre-configured with a requirement file that includes the right release. Example for transform developers working from git clone:\n",
    "```\n",
    "make venv \n",
    "source venv/bin/activate \n",
    "pip install jupyterlab\n",
    "venv/bin/jupyter lab\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c45c3c6-e4d7-4e61-8de6-32d61f2ce695",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "## This is here as a reference only\n",
    "# Users and application developers must use the right tag for the latest from pypi\n",
    "%pip install \"data-prep-toolkit-transforms[ray,filter]==1.1.0\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebf1f782-0e61-485c-8670-81066beb734c",
   "metadata": {},
   "source": [
    "##### **** Import required classes and modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c2a12abc-9460-4e45-8961-873b48a9ab19",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dpk_filter.ray.runtime import Filter\n",
    "from data_processing.utils import GB"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7234563c-2924-4150-8a31-4aec98c1bf33",
   "metadata": {},
   "source": [
    "\n",
    "##### **** Setup runtime parameters and invoke the transform. For a full list of parameters for the filter transform, please see [here](./README.md)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "95737436",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "09:51:08 INFO - pipeline id pipeline_id\n",
      "09:51:08 INFO - code location None\n",
      "09:51:08 INFO - number of workers 3 worker options {'num_cpus': 0.8, 'memory': 2147483648, 'max_restarts': -1}\n",
      "09:51:08 INFO - actor creation delay 0\n",
      "09:51:08 INFO - job details {'job category': 'preprocessing', 'job name': 'filter', 'job type': 'ray', 'job id': 'job_id'}\n",
      "09:51:08 INFO - data factory data_ is using local data access: input_folder - test-data/ds01/input/parquet output_folder - output/parquet\n",
      "09:51:08 INFO - data factory data_ max_files -1, n_sample -1\n",
      "09:51:08 INFO - data factory data_ Not using data sets, checkpointing False, max files -1, random samples -1, files to use ['.parquet'], files to checkpoint ['.parquet']\n",
      "09:51:08 INFO - Running locally\n",
      "2025-03-19 09:51:09,252\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n",
      "\u001b[36m(orchestrate pid=612)\u001b[0m 09:51:10 INFO - orchestrator started at 2025-03-19 09:51:10\n",
      "\u001b[36m(orchestrate pid=612)\u001b[0m 09:51:10 INFO - Number of files is 1, source profile {'max_file_size': 0.02131938934326172, 'min_file_size': 0.02131938934326172, 'total_file_size': 0.02131938934326172}\n",
      "\u001b[36m(orchestrate pid=612)\u001b[0m 09:51:10 INFO - Cluster resources: {'cpus': 10, 'gpus': 0, 'memory': 26.591864014044404, 'object_store': 2.0}\n",
      "\u001b[36m(orchestrate pid=612)\u001b[0m 09:51:10 INFO - Number of workers - 3 with {'num_cpus': 0.8, 'memory': 2147483648, 'max_restarts': -1} each\n",
      "\u001b[36m(orchestrate pid=612)\u001b[0m 09:51:11 INFO - Completed 0 files (0.0%)  in 0.0 min. Waiting for completion\n",
      "\u001b[36m(orchestrate pid=612)\u001b[0m 09:51:11 INFO - Completed processing 1 files in 0.0 min\n",
      "\u001b[36m(orchestrate pid=612)\u001b[0m 09:51:11 INFO - done flushing in 0.001 sec\n",
      "09:51:21 INFO - Completed execution in 0.22 min, execution result 0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#%%capture\n",
    "Filter(input_folder='test-data/ds01/input/parquet',\n",
    "            output_folder= 'output/parquet',\n",
    "            run_locally= True,\n",
    "            num_cpus= 0.8,\n",
    "            memory= 2 * GB,\n",
    "            runtime_num_workers = 3,\n",
    "            filter_columns_to_drop= [\"extra\", \"cluster\"],\n",
    "            filter_criteria_list= [\n",
    "                \"docq_total_words > 100 AND docq_total_words < 400\",\n",
    "                 \"ibmkenlm_docq_perplex_score < 250\",\n",
    "                ],\n",
    "            filter_input_arrow_folder= \"test-data/ds01/input/arrow\",\n",
    "            filter_output_arrow_folder = \"output/arrow\",\n",
    "            filter_doc_id_column_name = \"document_id\",\n",
    "            filter_logical_operator= \"AND\"\n",
    "            ).transform()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3df5adf-4717-4a03-864d-9151cd3f134b",
   "metadata": {},
   "source": [
    "##### **** The specified folder will include the transformed parquet files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7276fe84-6512-4605-ab65-747351e13a7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['output/arrow', 'output/parquet']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import glob\n",
    "glob.glob(\"output/*\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "845a75cf-f4a9-467d-87fa-ccbac1c9beb8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef43acef",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
